<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2019/3/9
 * Time: 16:55
 */

namespace app\admin\controller;

use app\admin\model\Rechange as RechangeModel;
use app\admin\validate\Rechange as RechangeValidate;
use think\db\Where;

class Rechange extends Base {
    public function __construct()
    {
        parent::__construct();
        $this->model = new RechangeModel;
        $this->validate = new RechangeValidate;
    }

    /**
     * @param int $limit
     * @param int $page
     * @return \think\Response|\think\response\View
     * @throws \think\exception\DbException
     */
    public function index($limit=10,$page=1){
        $where = new Where;
        $where['type']=2;
        $where['status']=['neq',1];
        if(self::$mc_id){
            $where['mc_id']=self::$mc_id;
        }
        $data = request()->post();
        if($data){
            if($data['out_trade_no']){
                $where['out_trade_no']=['eq',trim($data['out_trade_no'])];
            }
            if($data['status']){
                $where['status']=$data['status'];
            }
            if($data['date']){
                $t = explode('~',$data['date']);
                $start = strtotime($t[0]);
                $end = strtotime($t[1]);
                $where['create_time']= ['between time',[$start,$end]];
            }
        }
        $list = $this->model->getList($where,$limit);
        if(request()->isAjax()){
            return $this->__s('',$list);
        }else{
            $this->assign('total',$list->total());
            $this->assign('list',$list);
            return view();
        }
    }

    public function status($id=0,$status=0){
        if(!$id || !$status){
            $this->__e('缺少参数');
        }
        $mch = $this->model->get($id);
        if(!$mch){
            $this->__e('商户不存在');
        }
        $mch->status = $status;
        if(!$mch->isUpdate(true)->save()){
            $this->__e('操作失败');
        }
        $this->__s('操作成功');
    }


    /**
     * 删除
     * @param int $id
     * @throws \Exception
     */
    public function del($id=0){
        if(!$id){
            $this->__e('缺少参数');
        }
        if(!$this->model->where('id','eq',$id)->delete()){
            $this->__e('删除失败');
        }
        $this->__s('删除成功');
    }


    public function change(){
        $site = session('_site');
        $list = explode(',',$site['extends']);
        $this->assign('list',$list);
        $this->assign('selected',$list[0]);
        $this->assign('poundage',$site['poundage']*100);
        return view();
    }
}